package club.fuwenhao.demo;

import club.fuwenhao.utils.HttpUtils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fwh
 * @email fuwenhao594@163.com
 * @date 2022/1/29 10:25 下午
 */
@Slf4j
public class JDDataListener extends AnalysisEventListener<JDPrice> {
    private List<JDData> tempList = new ArrayList<>();
    private int count = 1;
    private int retryCount = 1;
    private String areaArg;

    public JDDataListener() {
    }

    public JDDataListener(String areaArg) {
        this.areaArg = areaArg;
    }

    @Override
    public void invoke(JDPrice jdPrice, AnalysisContext analysisContext) {
        //总行数
        Integer approximateTotalRowNumber = analysisContext.readSheetHolder().getApproximateTotalRowNumber();
        final Integer rowIndex = analysisContext.readRowHolder().getRowIndex();
        System.out.println("总行数：" + approximateTotalRowNumber + " 当前行数：" + rowIndex);
        JDData jdData = new JDData();
        final String skUid = jdPrice.getSKUid();
        jdData.setSkuID(skUid);
        //是否有货信息
        Map<String, String> resultMap = getIsStock(skUid, rowIndex);
        String isStock = "", serviceInfo = "", stockDesc = "", state = "", shopName = "", shopNameFlag = "";
        if (resultMap.containsKey("isStock")) {
            isStock = resultMap.get("isStock");
        }
        if (resultMap.containsKey("serviceInfo")) {
            serviceInfo = resultMap.get("serviceInfo");
        }
        if (resultMap.containsKey("stockDesc")) {
            stockDesc = resultMap.get("stockDesc");
        }
        if (resultMap.containsKey("state")) {
            state = resultMap.get("state");
        }
        if (resultMap.containsKey("shopName")) {
            shopName = resultMap.get("shopName");
        }
        if (resultMap.containsKey("shopNameFlag")) {
            shopNameFlag = resultMap.get("shopNameFlag");
        }
        if (!StringUtils.isEmpty(isStock)) {
            jdData.setIsStock(isStock);
            jdData.setServiceInfo(serviceInfo);
            jdData.setStockDesc(stockDesc);
            jdData.setShopName(shopName);
            jdData.setShopNameFlag(shopNameFlag);
        }
        if (!StringUtils.isEmpty(state)) {
            jdData.setState(state);
        }
        //评论数
        String comment = getComment(skUid, rowIndex);
        if (!StringUtils.isEmpty(comment)) {
            jdData.setCommentCount(comment);
        }
        //重新赋值价格
        if (resultMap.containsKey("p") && StringUtils.isEmpty(jdData.getPrice())) {
            jdData.setPrice(resultMap.get("p"));
        }
        tempList.add(jdData);
    }


    /**
     * 获取价格
     *
     * @param skUid
     * @return
     */
    private String getPrice(String skUid, Integer rowIndex) {
        String result = getPriceURL(skUid);
        if (null != result && !result.contains("error") && !result.contains("invalid")) {
            if (result.contains("jQuery")) {
                final String[] split = result.split("\\(");
                final String[] split1 = split[1].split("\\)");
                result = split1[0];
            }
            final JSONArray objects = JSONObject.parseArray(result);
            final String p = objects.getJSONObject(0).getString("p");
            log.info("获取价格成功：当前行：{},当前skuID为：{},价格为：{}", rowIndex, skUid, p);
            System.out.printf("获取价格成功：当前行：" + rowIndex + " 当前skuID为：" + skUid + " 价格为：" + p + "/\n");
            return p;
        } else {
            if (count < retryCount) {
                System.out.println("第" + count + "次获取价格失败：当前行：" + rowIndex + " 当前结果：" + result);
                count++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getPrice(skUid, rowIndex);
            }
//            log.error("获取价格失败：当前行：{},当前结果：{},请求URL地址为:{}", rowIndex, result, requestUrl);
//            System.out.println("获取价格失败：当前行：" + rowIndex + " 当前结果：" + result + "请求URL地址为:" + requestUrl);
            count = 0;
            return null;
        }
    }

    /**
     * 价格获取-url
     *
     * @param skUid
     * @return java.lang.String
     * @author fwh [2022/3/17 && 8:14 下午]
     */
    private String getPriceURL(String skUid) {
        StringBuffer stringBuffer = new StringBuffer();
//        stringBuffer.append("http://p.3.cn/prices/get?skuid=J_").append(skUid);
        stringBuffer.append("http://p.3.cn/prices/mgets?callback=jQuery8634257&type=1&area=5_264_266_49706&pdtk=&pduid=16285011738421400614125&pdpin=266971738-289848&pin=266971738-289848&pdbp=0&skuIds=J_").append(skUid).append("&ext=11100000&source=item-pc");
        Map<String, String> header = new HashMap<>();
        header.put("User-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36");
        header.put("Connection", "keep-alive");
        header.put("Accept", "*/*");
        header.put("Accept-Encoding", "gzip, deflate, sdch");
        header.put("Accept-Language", "zh-CN,zh;q=0.8");
        header.put("Host", "p.3.cn");
        header.put("Referer", "https://book.jd.com/booktop/0-0-0.html?category=1713-0-0-0-10001-1");

        final String requestUrl = stringBuffer.toString();
        String result = HttpUtils.getUrl(requestUrl, header);
        return result;
    }

    /**
     * 获取评论
     *
     * @return
     */
    private String getComment(String skUid, Integer rowIndex) {
//        Request URL: https://club.jd.com/comment/productCommentSummaries.action?referenceIds=71826090150
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("https://club.jd.com/comment/productCommentSummaries.action?referenceIds=")
                .append(skUid);
        String requestUrl = stringBuffer.toString();
        String reps = null;
        try {
            reps = HttpUtils.getUrlSkipSSL(requestUrl);
            if (StringUtils.isEmpty(reps)) {
                log.info("获取评论数失败：当前行：{},请求URL地址为:{}" + rowIndex, requestUrl);
                System.out.println("获取评论数失败：当前行：" + rowIndex + "请求URL地址为:" + requestUrl);
            } else {
                JSONObject jsonObject = JSONObject.parseObject(reps);
                String commentsCount = jsonObject.getString("CommentsCount");
                JSONArray objects = JSONObject.parseArray(commentsCount);
                String str = objects.get(0).toString();
                JSONObject commentJson = JSONObject.parseObject(str);
                String commentCountStr = commentJson.getString("CommentCountStr");
                log.info("获取评论数：当前行：{},当前skuID为：{},当前commentCountStr为：{}", rowIndex, skUid, commentCountStr);
                System.out.printf("获取评论数：当前行：" + rowIndex + " 当前skuID为：" + skUid + " 当前commentCountStr为：" + commentCountStr + "/\n");
                return commentCountStr;
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 是否有货
     *
     * @return
     */
    private Map<String, String> getIsStock(String skUid, Integer rowIndex) {
        Map<String, String> resultMap = new HashMap<>(4);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("https://item-soa.jd.com/getWareBusiness?skuId=").append(skUid).append("&cat=6196%2C11143%2C11148&area=");
        if (StringUtils.isEmpty(areaArg)) {
            stringBuffer.append("1_72_55653_0");//北京市朝阳区
            //                .append("1_2802_54745_0")//江苏无锡市
//                .append("21_1827_4101_40925")//江西南昌市
//                .append("25_2235_27497_52444")//云南昆明市
//                .append("1_72_55653_0")//北京市朝阳区
//                .append("10_698_707_46023")//黑龙江哈尔滨
        } else {
            stringBuffer.append(areaArg);
        }
        stringBuffer.append("&shopId=0&venderId=0&paramJson=%7B%22platform2%22%3A%221%22%2C%22colType%22%3A0%2C%22specialAttrStr%22%3A%22p0ppppppppppppppppppppp%22%2C%22skuMarkStr%22%3A%2200%22%7D&num=1");
        String requestUrl = stringBuffer.toString();
        String reps = null;
        try {
            reps = HttpUtils.getUrlSkipSSL(requestUrl);
            if (StringUtils.isEmpty(reps)) {
                log.info("获取是否有货失败：当前行：{},请求URL地址为:{}", rowIndex, requestUrl);
                System.out.println("获取是否有货失败：当前行：" + rowIndex + "请求URL地址为:" + requestUrl);
            } else if (reps.length() < 30) {
                if (count < retryCount) {
                    log.info("第" + count + "次获取是否有货失败：当前行：{},请求URL地址为:{}", rowIndex, requestUrl);
                    System.out.println("第" + count + "次获取是否有货失败：当前行：" + rowIndex + "请求URL地址为:" + requestUrl);
                    count++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    getIsStock(skUid, rowIndex);
                }
            } else {
                JSONObject jsonObject = JSONObject.parseObject(reps);
//                System.out.println("skuID为：" + skUid + " 发货信息响应为:" + jsonObject.toString());
                //获取商品信息
                JSONObject shopInfo = jsonObject.getJSONObject("shopInfo");
                if (!StringUtils.isEmpty(shopInfo)) {
                    JSONObject shop = shopInfo.getJSONObject("shop");
                    String shopName = shop.getString("name");
                    Boolean shopNameFlag = false;
                    if (shopName.contains("自营")) {
                        shopNameFlag = true;
                    }
                    System.out.println("skuID为：" + skUid + " shopName:" + shopName + "是否包含自营：" + String.valueOf(shopNameFlag));
                    resultMap.put("shopNameFlag", String.valueOf(shopNameFlag));
                    resultMap.put("shopName", shopName);
                }
                //获取库存发货信息
                JSONObject stockInfo = jsonObject.getJSONObject("stockInfo");
                if (!StringUtils.isEmpty(stockInfo)) {
                    String isStock = stockInfo.getString("isStock");
                    String serviceInfo = stockInfo.getString("serviceInfo");
                    String stockDesc = stockInfo.getString("stockDesc");
                    JSONObject deliveryInfo = stockInfo.getJSONObject("deliveryInfo");
                    String state = deliveryInfo.getString("state");
                    log.info("获取是否有货成功：当前行：{}, 当前skuID为：{},当前isStock为：{} ,当前state为：{},serviceInfo为：{}", rowIndex, skUid, isStock, state, serviceInfo);
                    System.out.printf("获取是否有货成功：当前行：" + rowIndex + " 当前skuID为：" + skUid + " 当前isStock为：" + isStock + "当前state为：" + state + " serviceInfo为:" + serviceInfo + " stockDesc为：{}" + stockDesc + "/\n");
                    resultMap.put("isStock", isStock);
                    resultMap.put("state", state);
                    resultMap.put("serviceInfo", serviceInfo);//会出现为空串的情况
                    resultMap.put("stockDesc", stockDesc);
                    //价格信息
                    JSONObject price = jsonObject.getJSONObject("price");
                    final String p = price.getString("p");
                    resultMap.put("p", p);
                    return resultMap;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        count = 0;
        return resultMap;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //输出Excel
        String fileName = "/Users/fwh/dev/" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
//        String fileName = "/Users/ext.tianlixia/dev/" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
//        String fileName = "C:/Users/14616/Desktop/" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel.write(fileName, JDData.class).build();
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            if (CollectionUtils.isEmpty(tempList)) {
                log.error("失败：数据为空。运行测试数据……");
                System.out.println("失败：数据为空。运行测试数据……");
                excelWriter.write(data(), writeSheet);
            } else {
                excelWriter.write(tempList, writeSheet);
            }
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
        log.info("程序运行结束……");
        System.out.println("程序运行结束……");
    }

    /**
     * 测试代码
     *
     * @return
     */
    private List<JDData> data() {
        List<JDData> list = new ArrayList<JDData>();
        for (int i = 0; i < 10; i++) {
            JDData data = new JDData();
            data.setSkuID("000" + i);
            data.setPrice("111" + i);
            list.add(data);
        }
        return list;
    }


    public static void main(String[] args) {

//        String reps = "{\n" +
//                "\"extendWarrantyInfo\": {\n" +
//                "\"descUrl\": \"https://baozhang.jd.com/static/serviceDesc\",\n" +
//                "\"detailUrl\": \"https://b.jr.jd.com/service/serveIntroduce/#/introduce3?mainSkuId={mainSkuId}&brandId={brandId}&thirdCategoryId={cid3}&bindSkuId={bindSku}\",\n" +
//                "\"serviceItems\": []\n" +
//                "},\n" +
//                "\"warmTips\": [\n" +
//                "{\n" +
//                "\"fontColor\": \"\",\n" +
//                "\"imageUrl\": \"http://m.360buyimg.com/mobilecms/jfs/t1918/274/1086575987/1970/bc766f2d/5680f4b3N24b48659.png\",\n" +
//                "\"text\": \"支持7天无理由退货\"\n" +
//                "}\n" +
//                "],\n" +
//                "\"rankUnited\": {},\n" +
//                "\"whiteBarInfo\": {\n" +
//                "\"creditStatus\": 1,\n" +
//                "\"isAva\": true,\n" +
//                "\"isBtUser\": true,\n" +
//                "\"isDiscountAll\": false,\n" +
//                "\"isFull\": true,\n" +
//                "\"isItemAva\": true,\n" +
//                "\"isLogin\": true,\n" +
//                "\"isShopAva\": true,\n" +
//                "\"isSkuAva\": true,\n" +
//                "\"key\": \"CAC116BD05F9F6EC4403171B25BB202D\",\n" +
//                "\"marketingText\": \"\",\n" +
//                "\"noAvaInfo\": \"\",\n" +
//                "\"planInfos\": [\n" +
//                "{\n" +
//                "\"curTotal\": 80,\n" +
//                "\"fee\": 0,\n" +
//                "\"feeText\": \"0服务费\",\n" +
//                "\"firstPay\": 80,\n" +
//                "\"firstRepayDate\": \"2022-03-15\",\n" +
//                "\"isDiscount\": false,\n" +
//                "\"isMaxDiscount\": false,\n" +
//                "\"laterPay\": 80,\n" +
//                "\"mainTitle\": \"不分期\",\n" +
//                "\"plan\": 1,\n" +
//                "\"planFee\": 0,\n" +
//                "\"rate\": 0,\n" +
//                "\"secondTitle\": \"无服务费\",\n" +
//                "\"total\": 80\n" +
//                "},\n" +
//                "{\n" +
//                "\"curTotal\": 27.22,\n" +
//                "\"fee\": 1.68,\n" +
//                "\"feeText\": \"含服务费\",\n" +
//                "\"firstPay\": 27.23,\n" +
//                "\"firstRepayDate\": \"2022-03-15\",\n" +
//                "\"isDiscount\": false,\n" +
//                "\"isMaxDiscount\": false,\n" +
//                "\"laterPay\": 27.22,\n" +
//                "\"mainTitle\": \"￥27.22 x 3期\",\n" +
//                "\"plan\": 3,\n" +
//                "\"planFee\": 0.56,\n" +
//                "\"rate\": 0.7,\n" +
//                "\"secondTitle\": \"含服务费：费率0.70%，￥0.56&times;3期\",\n" +
//                "\"total\": 81.68\n" +
//                "},\n" +
//                "{\n" +
//                "\"curTotal\": 13.91,\n" +
//                "\"fee\": 3.36,\n" +
//                "\"feeText\": \"含服务费\",\n" +
//                "\"firstPay\": 13.89,\n" +
//                "\"firstRepayDate\": \"2022-03-15\",\n" +
//                "\"isDiscount\": false,\n" +
//                "\"isMaxDiscount\": false,\n" +
//                "\"laterPay\": 13.91,\n" +
//                "\"mainTitle\": \"￥13.91 x 6期\",\n" +
//                "\"plan\": 6,\n" +
//                "\"planFee\": 0.56,\n" +
//                "\"rate\": 0.7,\n" +
//                "\"secondTitle\": \"含服务费：费率0.70%，￥0.56&times;6期\",\n" +
//                "\"total\": 83.36\n" +
//                "},\n" +
//                "{\n" +
//                "\"curTotal\": 7.19,\n" +
//                "\"fee\": 6.72,\n" +
//                "\"feeText\": \"含服务费\",\n" +
//                "\"firstPay\": 7.23,\n" +
//                "\"firstRepayDate\": \"2022-03-15\",\n" +
//                "\"isDiscount\": false,\n" +
//                "\"isMaxDiscount\": false,\n" +
//                "\"laterPay\": 7.19,\n" +
//                "\"mainTitle\": \"￥7.19 x 12期\",\n" +
//                "\"plan\": 12,\n" +
//                "\"planFee\": 0.56,\n" +
//                "\"rate\": 0.7,\n" +
//                "\"secondTitle\": \"含服务费：费率0.70%，￥0.56&times;12期\",\n" +
//                "\"total\": 86.72\n" +
//                "},\n" +
//                "{\n" +
//                "\"curTotal\": 3.97,\n" +
//                "\"fee\": 13.44,\n" +
//                "\"feeText\": \"含服务费\",\n" +
//                "\"firstPay\": 3.89,\n" +
//                "\"firstRepayDate\": \"2022-03-15\",\n" +
//                "\"isDiscount\": false,\n" +
//                "\"isMaxDiscount\": false,\n" +
//                "\"laterPay\": 3.97,\n" +
//                "\"mainTitle\": \"￥3.97 x 24期\",\n" +
//                "\"plan\": 24,\n" +
//                "\"planFee\": 0.56,\n" +
//                "\"rate\": 0.7,\n" +
//                "\"secondTitle\": \"含服务费：费率0.70%，￥0.56&times;24期\",\n" +
//                "\"total\": 93.44\n" +
//                "}\n" +
//                "],\n" +
//                "\"propMarketText\": \"\",\n" +
//                "\"url\": \"//bt.jd.com/v3/activity/open?rid=1644914439648\",\n" +
//                "\"ver\": \"1\"\n" +
//                "},\n" +
//                "\"hasPlusBalance\": false,\n" +
//                "\"promiseFxgInfo\": {\n" +
//                "\"fxgCode\": \"0\",\n" +
//                "\"oldTsfwCodes\": \"p1,\"\n" +
//                "},\n" +
//                "\"hasWarranty\": false,\n" +
//                "\"isInstallNow\": false,\n" +
//                "\"price\": {\n" +
//                "\"epp\": \"\",\n" +
//                "\"hagglePromotion\": false,\n" +
//                "\"id\": \"10041387110015\",\n" +
//                "\"m\": \"425.00\",\n" +
//                "\"nup\": \"\",\n" +
//                "\"op\": \"80.00\",\n" +
//                "\"p\": \"80.00\",\n" +
//                "\"plusTag\": {\n" +
//                "\"limit\": false,\n" +
//                "\"min\": 0,\n" +
//                "\"max\": 0,\n" +
//                "\"overlying\": false\n" +
//                "},\n" +
//                "\"pp\": \"\",\n" +
//                "\"sdp\": \"\",\n" +
//                "\"sfp\": \"\",\n" +
//                "\"sp\": \"\",\n" +
//                "\"tkp\": \"\",\n" +
//                "\"tpp\": \"\"\n" +
//                "},\n" +
//                "\"adText\": \"店铺满减优惠进行中下单即送精美小礼品定制商品请联系客服确认出货时间偏远地区运费为30均需要咨询客服补运费本店满六十元开发票\",\n" +
//                "\"shopInfo\": {\n" +
//                "\"shop\": {\n" +
//                "\"afterSaleGrade\": \"低\",\n" +
//                "\"afterSaleScore\": \"8.26\",\n" +
//                "\"afterSaleTxt\": \"售后\",\n" +
//                "\"avgEfficiencyScore\": 0,\n" +
//                "\"avgServiceScore\": 0,\n" +
//                "\"avgWareScore\": 0,\n" +
//                "\"brief\": \"专属好礼定制 婚庆用品 节日礼物\",\n" +
//                "\"cardType\": 1,\n" +
//                "\"cateGoodShop\": 0,\n" +
//                "\"diamond\": false,\n" +
//                "\"efficiencyScore\": 0,\n" +
//                "\"evaluateGrade\": \"高\",\n" +
//                "\"evaluateScore\": \"10.00\",\n" +
//                "\"evaluateTxt\": \"评价\",\n" +
//                "\"followCount\": 230,\n" +
//                "\"followText\": \"关注人数\",\n" +
//                "\"giftIcon\": \"\",\n" +
//                "\"goodShop\": 0,\n" +
//                "\"hasCoupon\": false,\n" +
//                "\"isSquareLogo\": false,\n" +
//                "\"logisticsGrade\": \"中\",\n" +
//                "\"logisticsScore\": \"9.14\",\n" +
//                "\"logisticsText\": \"物流\",\n" +
//                "\"logisticsTxt\": \"物流\",\n" +
//                "\"logo\": \"http://img30.360buyimg.com/popshop/jfs/t1/148874/28/8658/19476/5f64d871Ee7cab81c/3fbd36e74bb680bd.png\",\n" +
//                "\"name\": \"梓茈尔山专卖店\",\n" +
//                "\"nameB\": \"梓茈尔山专卖店\",\n" +
//                "\"newNum\": 0,\n" +
//                "\"promotionNum\": 0,\n" +
//                "\"score\": 0,\n" +
//                "\"scoreRankRateGrade\": \"4.5\",\n" +
//                "\"scoreText\": \"综合评分\",\n" +
//                "\"serverText\": \"服务\",\n" +
//                "\"serviceScore\": 0,\n" +
//                "\"shopActivityTotalNum\": 0,\n" +
//                "\"shopId\": 10281252,\n" +
//                "\"shopStarTxt\": \"店铺星级\",\n" +
//                "\"shopStateText\": \"店铺动态\",\n" +
//                "\"signboardUrl\": \"http://img11.360buyimg.com/jshopm/jfs/t1198/177/246925151/74805/ab73d78b/550ffd76Nf0a0bb07.png\",\n" +
//                "\"skuCntText\": \"全部商品\",\n" +
//                "\"skuText\": \"商品\",\n" +
//                "\"telephone\": \"\",\n" +
//                "\"totalNum\": 1738,\n" +
//                "\"venderType\": \"0\",\n" +
//                "\"wareScore\": 0\n" +
//                "},\n" +
//                "\"customerService\": {\n" +
//                "\"hasChat\": false,\n" +
//                "\"hasJimi\": false,\n" +
//                "\"mLink\": \"http://m.jd.com/product/10041387110015.html\",\n" +
//                "\"online\": false\n" +
//                "}\n" +
//                "},\n" +
//                "\"ad\": \"店铺满减优惠进行中下单即送精美小礼品定制商品请联系客服确认出货时间偏远地区运费为30均需要咨询客服补运费本店满六十元开发票\",\n" +
//                "\"servicesInfoUnited\": {\n" +
//                "\"hasWarranty\": false,\n" +
//                "\"icon\": true,\n" +
//                "\"isSupport\": 1,\n" +
//                "\"promiseFxgInfo\": {\n" +
//                "\"fxgCode\": \"0\",\n" +
//                "\"oldTsfwCodes\": \"p1,\"\n" +
//                "},\n" +
//                "\"servIconRelations\": [\n" +
//                "{\n" +
//                "\"iconType\": \"exclamation\",\n" +
//                "\"iconValue\": \"detail_006\"\n" +
//                "},\n" +
//                "{\n" +
//                "\"iconType\": \"right\",\n" +
//                "\"iconValue\": \"detail_005\"\n" +
//                "}\n" +
//                "],\n" +
//                "\"serviceInfo\": {\n" +
//                "\"basic\": {\n" +
//                "\"iconList\": [],\n" +
//                "\"title\": \"服务说明\"\n" +
//                "}\n" +
//                "},\n" +
//                "\"stockInfo\": {\n" +
//                "\"deliveryInfo\": {},\n" +
//                "\"isPlus\": false,\n" +
//                "\"code\": 1,\n" +
//                "\"serviceInfo\": \"由<a href='http://mall.jd.com/index-10281252.html' target='_blank' clstag='shangpin|keycount|product|bbtn' class='hl_red'>梓茈尔山专卖店</a>发货, 并提供售后服务. \",\n" +
//                "\"stockDesc\": \"<strong>无货</strong>，此商品暂时售完\",\n" +
//                "\"dcashDesc\": \"运费30元 <a title=\\\"了解配送费收取标准\\\" href=\\\"//help.jd.com/user/issue/109-188.html\\\" target=\\\"_blank\\\"><i class=\\\"sprite-question\\\"></i></a>\",\n" +
//                "\"fxgCode\": \"0\",\n" +
//                "\"stockState\": 34,\n" +
//                "\"stockInfo\": {},\n" +
//                "\"serverIcon\": {},\n" +
//                "\"support\": [],\n" +
//                "\"isStock\": false\n" +
//                "},\n" +
//                "\"wareExtendWarrantyInfo\": {\n" +
//                "\"descUrl\": \"https://baozhang.jd.com/static/serviceDesc\",\n" +
//                "\"detailUrl\": \"https://b.jr.jd.com/service/serveIntroduce/#/introduce3?mainSkuId={mainSkuId}&brandId={brandId}&thirdCategoryId={cid3}&bindSkuId={bindSku}\",\n" +
//                "\"serviceItems\": []\n" +
//                "},\n" +
//                "\"wareStockInfo\": {\n" +
//                "\"is7ToReturn\": true,\n" +
//                "\"is7shortService\": \"七天退货\"\n" +
//                "},\n" +
//                "\"warmTips\": [\n" +
//                "{\n" +
//                "\"fontColor\": \"\",\n" +
//                "\"imageUrl\": \"http://m.360buyimg.com/mobilecms/jfs/t1918/274/1086575987/1970/bc766f2d/5680f4b3N24b48659.png\",\n" +
//                "\"text\": \"支持7天无理由退货\"\n" +
//                "}\n" +
//                "]\n" +
//                "},\n" +
//                "\"couponInfo\": [],\n" +
//                "\"baitiaoPlanShowResVo\": \"{\\\"act\\\":0,\\\"bp\\\":1,\\\"creditStatus\\\":1,\\\"isAva\\\":true,\\\"isBtUser\\\":true,\\\"isDiscountAll\\\":false,\\\"isFull\\\":true,\\\"isItemAva\\\":true,\\\"isLogin\\\":true,\\\"isScc\\\":false,\\\"isShopAva\\\":true,\\\"isSkuAva\\\":true,\\\"key\\\":\\\"CAC116BD05F9F6EC4403171B25BB202D\\\",\\\"marketingText\\\":\\\"\\\",\\\"noAvaInfo\\\":\\\"\\\",\\\"planInfos\\\":[{\\\"curTotal\\\":80.00,\\\"fee\\\":0.00,\\\"firstPay\\\":80.00,\\\"firstRepayDate\\\":\\\"2022-03-15\\\",\\\"isDiscount\\\":false,\\\"laterPay\\\":80.00,\\\"maxDiscount\\\":true,\\\"plan\\\":1,\\\"planFee\\\":0.00,\\\"rate\\\":0.00,\\\"total\\\":80.00},{\\\"curTotal\\\":27.22,\\\"fee\\\":1.68,\\\"firstPay\\\":27.23,\\\"firstRepayDate\\\":\\\"2022-03-15\\\",\\\"isDiscount\\\":false,\\\"laterPay\\\":27.22,\\\"maxDiscount\\\":false,\\\"plan\\\":3,\\\"planFee\\\":0.56,\\\"rate\\\":0.70,\\\"total\\\":81.68},{\\\"curTotal\\\":13.91,\\\"fee\\\":3.36,\\\"firstPay\\\":13.89,\\\"firstRepayDate\\\":\\\"2022-03-15\\\",\\\"isDiscount\\\":false,\\\"laterPay\\\":13.91,\\\"maxDiscount\\\":false,\\\"plan\\\":6,\\\"planFee\\\":0.56,\\\"rate\\\":0.70,\\\"total\\\":83.36},{\\\"curTotal\\\":7.19,\\\"fee\\\":6.72,\\\"firstPay\\\":7.23,\\\"firstRepayDate\\\":\\\"2022-03-15\\\",\\\"isDiscount\\\":false,\\\"laterPay\\\":7.19,\\\"maxDiscount\\\":false,\\\"plan\\\":12,\\\"planFee\\\":0.56,\\\"rate\\\":0.70,\\\"total\\\":86.72},{\\\"curTotal\\\":3.97,\\\"fee\\\":13.44,\\\"firstPay\\\":3.89,\\\"firstRepayDate\\\":\\\"2022-03-15\\\",\\\"isDiscount\\\":false,\\\"laterPay\\\":3.97,\\\"maxDiscount\\\":false,\\\"plan\\\":24,\\\"planFee\\\":0.56,\\\"rate\\\":0.70,\\\"total\\\":93.44}],\\\"propMarketText\\\":\\\"\\\",\\\"result\\\":{\\\"code\\\":\\\"00000\\\",\\\"info\\\":\\\"success\\\",\\\"success\\\":true},\\\"sccBt\\\":false,\\\"ver\\\":\\\"1\\\"}\",\n" +
//                "\"warrantyInfo\": {\n" +
//                "\"descUrl\": \"https://baozhang.jd.com/static/serviceDesc\",\n" +
//                "\"detailUrl\": \"https://b.jr.jd.com/service/serveIntroduce/#/introduce3?mainSkuId={mainSkuId}&brandId={brandId}&thirdCategoryId={cid3}&bindSkuId={bindSku}\",\n" +
//                "\"serviceItems\": []\n" +
//                "},\n" +
//                "\"shopUrl\": \"http://mall.jd.com/index-10281252.html\",\n" +
//                "\"giftShoppingInfo\": {\n" +
//                "\"gaoduanGift\": false,\n" +
//                "\"hasGift\": false,\n" +
//                "\"huodongGift\": false\n" +
//                "},\n" +
//                "\"deliveryPlace\": \"\",\n" +
//                "\"hasFinanceCoupon\": false,\n" +
//                "\"jdddFlag\": false,\n" +
//                "\"isSamMember\": false,\n" +
//                "\"stockInfo\": {\n" +
//                "\"deliveryInfo\": {\n" +
//                "\"state\": \"SUPPORT\",\n" +
//                "\"support\": true\n" +
//                "},\n" +
//                "\"isPlus\": false,\n" +
//                "\"code\": 1,\n" +
//                "\"serviceInfo\": \"由<a href='http://mall.jd.com/index-10281252.html' target='_blank' clstag='shangpin|keycount|product|bbtn' class='hl_red'>梓茈尔山专卖店</a>发货, 并提供售后服务. \",\n" +
//                "\"stockDesc\": \"<strong>无货</strong>，此商品暂时售完\",\n" +
//                "\"dcashDesc\": \"运费30元 <a title=\\\"了解配送费收取标准\\\" href=\\\"//help.jd.com/user/issue/109-188.html\\\" target=\\\"_blank\\\"><i class=\\\"sprite-question\\\"></i></a>\",\n" +
//                "\"fxgCode\": \"0\",\n" +
//                "\"stockState\": 34,\n" +
//                "\"stockInfo\": {\n" +
//                "\"ae\": 0,\n" +
//                "\"ah\": -1,\n" +
//                "\"availableNum\": -1,\n" +
//                "\"businessType\": -1,\n" +
//                "\"ca\": -1,\n" +
//                "\"date\": \"\",\n" +
//                "\"dcId\": -1,\n" +
//                "\"dcIdDely\": -1,\n" +
//                "\"ef\": 0,\n" +
//                "\"eh\": -1,\n" +
//                "\"freshEdi\": 0,\n" +
//                "\"popPatType\": -1,\n" +
//                "\"preStore\": -1,\n" +
//                "\"railModel\": 0,\n" +
//                "\"reservationType\": \"OTHER\",\n" +
//                "\"rid\": 0,\n" +
//                "\"sidDely\": -1,\n" +
//                "\"siteId4Dada\": -1,\n" +
//                "\"stDely\": -1,\n" +
//                "\"stockM\": 0,\n" +
//                "\"stockState\": \"DISABLE\",\n" +
//                "\"stockV\": 0,\n" +
//                "\"storeId\": -1,\n" +
//                "\"storeId4Dada\": -1,\n" +
//                "\"storeState4Dada\": -1,\n" +
//                "\"storeType\": -1,\n" +
//                "\"useStockNum\": 99\n" +
//                "},\n" +
//                "\"serverIcon\": {\n" +
//                "\"wlfwIcons\": [],\n" +
//                "\"basicIcons\": []\n" +
//                "},\n" +
//                "\"support\": [],\n" +
//                "\"isStock\": false\n" +
//                "},\n" +
//                "\"userInfoMap\": {\n" +
//                "\"flagInfoByPin\": \"0000000000000000203000000000010900101000000000006300001000000080000000000000000000000000000000000000\",\n" +
//                "\"idAuthThrough\": false,\n" +
//                "\"jingXiangScore\": 5756,\n" +
//                "\"plusMember\": \"4\",\n" +
//                "\"samMember\": false,\n" +
//                "\"unJdUserFlag\": false\n" +
//                "},\n" +
//                "\"isPlusMember\": \"4\",\n" +
//                "\"wozheFlag\": false,\n" +
//                "\"isJdkd\": false,\n" +
//                "\"actions\": {},\n" +
//                "\"isJdwl\": false,\n" +
//                "\"promotion\": {\n" +
//                "\"activity\": [],\n" +
//                "\"attach\": [],\n" +
//                "\"canReturnHaggleInfo\": false,\n" +
//                "\"customtag\": {},\n" +
//                "\"gift\": [],\n" +
//                "\"giftTips\": \"\",\n" +
//                "\"isBargain\": false,\n" +
//                "\"isTwoLine\": false,\n" +
//                "\"limitBuyInfo\": {\n" +
//                "\"limitNum\": \"0\",\n" +
//                "\"noSaleFlag\": \"0\",\n" +
//                "\"resultExt\": {\n" +
//                "\"isPlusLimit\": \"0\"\n" +
//                "}\n" +
//                "},\n" +
//                "\"normalMark\": \"tab_var_071\",\n" +
//                "\"plusMark\": \"tab_var_124\",\n" +
//                "\"prompt\": \"\",\n" +
//                "\"screenLiPurMap\": {},\n" +
//                "\"tip\": \"\",\n" +
//                "\"tips\": [],\n" +
//                "\"upgradePurchaseMap\": {}\n" +
//                "}\n" +
//                "}";

//        String reps = "";
//        JSONObject jsonObject = JSONObject.parseObject(reps);
//        JSONObject stockInfo = jsonObject.getJSONObject("stockInfo");
//        System.out.println(stockInfo.getString("isStock"));
//
//        JSONObject deliveryInfo = stockInfo.getJSONObject("deliveryInfo");
//        String state = deliveryInfo.getString("state");
//        System.out.println(state);


        String comment = "{\n" +
                "\"CommentsCount\": [\n" +
                "{\n" +
                "\"SkuId\": 71826090150,\n" +
                "\"ProductId\": 71826090150,\n" +
                "\"ShowCount\": 0,\n" +
                "\"ShowCountStr\": \"200+\",\n" +
                "\"CommentCountStr\": \"5000+\",\n" +
                "\"CommentCount\": 0,\n" +
                "\"AverageScore\": 5,\n" +
                "\"DefaultGoodCountStr\": \"5000+\",\n" +
                "\"DefaultGoodCount\": 0,\n" +
                "\"GoodCountStr\": \"1000+\",\n" +
                "\"GoodCount\": 0,\n" +
                "\"AfterCount\": 0,\n" +
                "\"OneYear\": 0,\n" +
                "\"AfterCountStr\": \"20+\",\n" +
                "\"VideoCount\": 0,\n" +
                "\"VideoCountStr\": \"5\",\n" +
                "\"GoodRate\": 0.96,\n" +
                "\"GoodRateShow\": 96,\n" +
                "\"GoodRateStyle\": 144,\n" +
                "\"GeneralCountStr\": \"20+\",\n" +
                "\"GeneralCount\": 0,\n" +
                "\"GeneralRate\": 0.018,\n" +
                "\"GeneralRateShow\": 2,\n" +
                "\"GeneralRateStyle\": 3,\n" +
                "\"PoorCountStr\": \"20+\",\n" +
                "\"PoorCount\": 0,\n" +
                "\"SensitiveBook\": 0,\n" +
                "\"PoorRate\": 0.022,\n" +
                "\"PoorRateShow\": 2,\n" +
                "\"PoorRateStyle\": 3\n" +
                "}\n" +
                "]\n" +
                "}";
        JSONObject jsonObject = JSONObject.parseObject(comment);
        String commentsCount = jsonObject.getString("CommentsCount");
        JSONArray objects = JSONObject.parseArray(commentsCount);
        String str = objects.get(0).toString();
        JSONObject commentJson = JSONObject.parseObject(str);
        System.out.println(commentJson.getString("CommentCountStr"));


//        try {
//            getIsStock();
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        } catch (KeyManagementException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
}
